<?php

/*
 * Niken Larasati
 * larasati.n@students.amikom.ac.id
 */

class kk extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('tanggal');
    }

    function form_kk() {
        $this->load->model('umum_m');
        $data['propinsi'] = $this->umum_m->ambil_data('propinsi');
        $this->template->load('halaman_v', 'kk/tambah_kk_v', $data);
    }

    function tambah() { //diurutkan basisdata = form
        if ($this->input->post('tambah') != NULL) {
            $data['no_kk'] = $this->input->post('no_kk');
            $data['alamat'] = $this->input->post('alamat');
            $data['dusun'] = $this->input->post('dusun');
            $data['rt'] = $this->input->post('rt');
            $data['rw'] = $this->input->post('rw');
            $data['desa'] = $this->input->post('desa');
            $data['kecamatan'] = $this->input->post('kecamatan');
            $data['kabupaten'] = $this->input->post('kabupaten');
            $data['idpropinsi'] = $this->input->post('idpropinsi');
            $data['kodepos'] = $this->input->post('kodepos');
            $data['tgl_pembuatan'] = $this->input->post('tgl_pembuatan');

            $this->load->model('umum_m');
            $this->umum_m->tambah('kk', $data);
            redirect('kk/tampil');
        }
    }

    function ubah($id = NULL) {
        if ($id != NULL) {
            $this->load->model('umum_m');
            $data['data_kk'] = $this->umum_m->ambil_data('kk', 'no_kk = ' . $id)->row();
            $this->template->load('halaman_v', 'kk/ubah_kk_v', $data);
        } else {
            redirect('kk');
        }
    }

    function simpan_ubah() { //diurutkan basisdata = form
        if ($this->input->post() != NULL) {
            $kk['no_kk'] = $this->input->post('no_kk');
            $kk['alamat'] = $this->input->post('alamat');
            $kk['dusun'] = $this->input->post('dusun');
            $kk['rt'] = $this->input->post('rt');
            $kk['rw'] = $this->input->post('rw');
//            $kk['desa'] = $this->input->post('desa');
//            $kk['kecamatan'] = $this->input->post('kecamatan');
//            $kk['kabupaten'] = $this->input->post('kabupaten');
//            $kk['kodepos'] = $this->input->post('kodepos');
//            $kk['idpropinsi'] = $this->input->post('idpropinsi');
            $kk['tgl_pembuatan'] = $this->input->post('tgl_pembuatan');

            $this->load->model('umum_m');
            if ($this->umum_m->ubah('kk', $kk, 'no_kk', $kk['no_kk'])) {
                redirect('kk/tampil');
            }
        }
    }

    function tampil() {
        $this->load->model('kk_m');
        $data['kk'] = $this->kk_m->ambil_data();
        $this->template->load('halaman_v', 'kk/tampil_kk_v', $data);
    }

    function hapus($id = NULL) {
        if ($id != NULL) {
            $this->load->model('umum_m');
            $status = $this->umum_m->cek_relasi_tabel('penduduk', 'no_kk', $id);
            $status = $this->umum_m->cek_relasi_tabel('kelahiran', 'no_kk', $id);
            $status = $this->umum_m->cek_relasi_tabel('kematian', 'no_kk', $id);
            $status = $this->umum_m->cek_relasi_tabel('pindah_pergi', 'no_kk', $id);
            if ($status == 0) {
                $this->umum_m->hapus_data_tabel('kk', array('no_kk' => $id));
                $this->tampil();
            }
        }
    }

    function cari_tahun() {
        $this->load->model('kk_m');
        $tahun['kk'] = $this->kk_m->ambil_data();
        $this->template->load('halaman_v', 'kk/cari_tahunkk_v', $tahun);
    }

    function cari_bulan() {
        $this->load->model('kk_m');
        $bulan['kk'] = $this->kk_m->ambil_data();
        $this->template->load('halaman_v', 'kk/cari_bulankk_v', $bulan);
    }
    
    function cari_nokk() {
        $this->load->model('umum_m');
        $data['kk'] = $this->umum_m->ambil_data('kk');
        if ($this->input->post() != NULL) {
            $this->load->model('kk_m');
            $data['kk'] = $this->kk_m->detail_cari_nokk('kk.no_kk', $this->input->post('no_kk'));
            $data['nomor_kk'] = $this->input->post('no_kk');
            $this->template->load('halaman_v', 'kk/cari_nokk_v', $data);
        } else {
            $this->load->model('kk_m');
            $data['kk'] = $this->kk_m->detail_cari_nokk();
            $this->template->load('halaman_v', 'kk/cari_nokk_v', $data);
        }
    }

    function cari_dusun() {
        $this->load->model('umum_m');
        $data['kk'] = $this->umum_m->ambil_data('kk');
        $data['penduduk'] = $this->umum_m->ambil_data('penduduk');
        if ($this->input->post() != NULL) {
            $this->load->model('kk_m');
            $this->load->model('penduduk_m');
            $data['kk'] = $this->kk_m->detail_cari('kk.dusun',  $this->input->post('dusun'));
            //$data['penduduk'] = $this->kk_m->detail_cari();
            $data['dusun_kk'] = $this->input->post('dusun');
            $this->template->load('halaman_v', 'kk/cari_dusunkk_v', $data);
        } else {
            $this->load->model('kk_m');
            $data['kk'] = $this->kk_m->detail_cari();
            $this->template->load('halaman_v', 'kk/cari_dusunkk_v', $data);
        }
    }
        
        function cetak_nokk() {
        $this->load->model('umum_m');
        $data['kk'] = $this->umum_m->ambil_data('kk');
        if ($this->input->post() != NULL) {
            $this->load->model('kk_m');
            $data['kk'] = $this->kk_m->detail_cari_nokk('kk.no_kk', $this->input->post('no_kk'));
            $data['nomor_kk'] = $this->input->post('no_kk');
            $this->template->load('halaman_v', 'kk/cetak_nokk_v', $data);
        } else {
            $this->load->model('kk_m');
            $data['kk'] = $this->kk_m->detail_cari_nokk();
            $this->template->load('halaman_v', 'kk/cetak_nokk_v', $data);
        }
    }

    
}

?>
